﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Backend.JYD.Model;

namespace Backend.JYD.RunTime
{
    public class BackendModule : IHttpModule
    {
        private EmpInfo m_EmpInfo = null;
        public void Init(HttpApplication context)
        {
            //开始请求，获取用户信息
            context.BeginRequest += new EventHandler(context_BeginRequest);
            //验证用户信息
            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
            //授权，获取用户权限
            context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
            //context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);
            //context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            //context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
            //context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache);
            context.EndRequest += new EventHandler(context_EndRequest);
            //context.Error += new EventHandler(context_Error);
        }

        protected void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            //if (context.Request.HttpMethod.ToLower() != "post")
            //{
                bool login = this.IsLoginUrl(context);
                if (login) return;
            //}
            //this.m_EmpInfo = SessionMgr.GetEmpInfoFromSession(context);
            //if (this.m_EmpInfo == null)
            //{ 
                //跳转到登陆页
                context.Response.Redirect("/home/index", true);
            //}
        }
        protected void context_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            if (context.Request.HttpMethod.ToLower() != "post")
            {
                bool login = this.IsLoginUrl(context);
                if (login) return;
            }
        }

        protected void context_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            if (context.Request.HttpMethod.ToLower() != "post")
            {
                bool login = this.IsLoginUrl(context);
                if (login) return;
            }
        }
        protected void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            if (context.Request.HttpMethod.ToLower() != "post")
            {
                bool login = this.IsLoginUrl(context);
                if (login) return;
            }
            //SessionMgr.ResetSession(context, this.m_EmpInfo);
        }
        
        public void Dispose()
        { }

        private bool IsLoginUrl(HttpContext content)
        {
            return content.Request.Url.AbsolutePath == "/home/index" || content.Request.Url.AbsolutePath == "/";
        }
    }
}
